﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SERP.StringMappings;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.People
{
    public partial class PeopleViewInfo : SERP.Modules.ModuleBase
    {
        private PeopleController controller = new PeopleController();
        public PeopleController Controller
        {
            get
            {
                return controller;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadLanguage();
            if (!this.IsPostBack)
            {
                BindInfo();
            }
        }
        //Load All lable text
        private void LoadLanguage()
        {
            labelPersonCode.Text = PeopleStringMapping.PersonCode + ":";
            labelFullName.Text = PeopleStringMapping.FullName + ":";
            labelGender.Text = PeopleStringMapping.Gender + ":";
            labelBirthDay.Text = PeopleStringMapping.BirthDay + ":";
            labelAddress.Text = PeopleStringMapping.Address + ":";
            labelPhone.Text = PeopleStringMapping.Phone + ":";
            labelEmail.Text = PeopleStringMapping.Email + ":";
            labelPositionId.Text = PeopleStringMapping.PositionId + ":";
            labelStartDate.Text = PeopleStringMapping.StartDate + ":";
            labelNote.Text = PeopleStringMapping.Note + ":";
            labelUserId.Text = PeopleStringMapping.UserId + ":";
            labelOrganizationId.Text = PeopleStringMapping.OrganizationId + ":";
            labelPersonTypeId.Text = PeopleStringMapping.PersonTypeId + ":";
        }
        public PeopleEntity GetObject()
        {
            controller.LoadObject(Int32.Parse(Request["PersonId"]));
            return controller.PeopleObject;
        }
        private void BindInfo()
        {
            PeopleEntity o = GetObject();
            CopyToSreen(o);
            BindOrganizationId(o);
            BindPersonTypeId(o);
            BindPosition(o);
            BindUsers(o);
        }
        private void BindOrganizationId(PeopleEntity o)
        {
            OrganizationsController ctrlOrganizations = new OrganizationsController();
            ctrlOrganizations.LoadObject(o.OrganizationId);
            OrganizationsEntity oOrganizations = ctrlOrganizations.OrganizationsObject;
            if (oOrganizations != null)
            {
                txtOrganizationId.Text = oOrganizations.OrgName.ToString();
            }
            else
            {
                txtOrganizationId.Text = Constants.APP_NOTSET;
            }
        }
        private void BindPersonTypeId(PeopleEntity o)
        {
            PersonTypesController ctrlPersonTypes = new PersonTypesController();
            ctrlPersonTypes.LoadObject(o.PersonTypeId);
            PersonTypesEntity oPersonTypes = ctrlPersonTypes.PersonTypesObject;
            if (oPersonTypes != null)
            {
                txtPersonTypeId.Text = oPersonTypes.TypeName.ToString();
            }
            else
            {
                txtPersonTypeId.Text = Constants.APP_NOTSET;
            }
        }
        private void BindPosition(PeopleEntity o)
        {
            WorkPositionsController ctrlWorkPositions = new WorkPositionsController();
            ctrlWorkPositions.LoadObject(o.PositionId);
            WorkPositionsEntity oWorkPositions = ctrlWorkPositions.WorkPositionsObject;
            if (oWorkPositions != null)
            {
                txtPositionId.Text = oWorkPositions.PosName.ToString();
            }
            else
            {
                txtPositionId.Text = Constants.APP_NOTSET;
            }
        }
        private void BindUsers(PeopleEntity o)
        {
            UsersController ctrlUsers = new UsersController();
            ctrlUsers.LoadObject(o.PositionId);
            UsersEntity oUsers = ctrlUsers.UsersObject;
            if (oUsers != null)
            {
                txtUserId.Text = oUsers.UserName.ToString();
            }
            else
            {
                txtUserId.Text = Constants.APP_NOTSET;
            }
        }
        private void CopyToSreen(PeopleEntity o)
        {
            txtPersonCode.Text = o.PersonCode;
            txtFullName.Text = o.FullName;
            txtGender.Text = o.Gender == 1 ? Constants.APP_MALE : Constants.APP_FEMALE;
            txtBirthDay.Text = o.BirthDay.ToString(SERP.Constants.APP_DATE_FORMAT);
            txtAddress.Text = o.Address;
            txtPhone.Text = o.Phone;
            txtEmail.Text = o.Email;            
            txtStartDate.Text = o.StartDate.ToString(SERP.Constants.APP_DATE_FORMAT);
            txtNote.Text = o.Note;
        }
    }
}
